package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.List;

/**
 * 做逻辑关系的
 */
public class UserServiceImpl implements UserService {
    @Override
    public String add(UserModel userModel) {
//1.根据当前的用户名去查询是否已经注册
//        查询的返回结果，要吗有要么没有
//        2.如果已经注册，提示用户重新输入用户名
//        3.如果没有注册，则直接新增数据，提示注册成功
        UserDao userDao = new UserImpl();
        UserModel model = userDao.findByUsername(userModel.getUsername());
        if (model != null) {
            return "isHave";
        }
        //表示没有注册，则直接注册
//新增一条数据，返回的是影响的条数  整型
        int a = userDao.add(userModel);//
//        if (a > 0) {
//            return "success";
//        }
//        return "error";
        return a > 0 ? "success" : "error";
    }

    /**
     * 1.先判断验证码是否正确
     * 2.如果正确，根据用户名和密码查询
     * 3.如果有值，则返回success
     * 4.如果没有值，则返回error
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
//        equals和==之间的区别？

        if (!userModel.getCode().equals(userModel.getSessionCode())) {
            return "error-code";
        }
        UserDao userDao = new UserImpl();
        UserModel model = userDao.findByUsernameAndPassword(userModel);
        if (model == null) {
            return "error";
        }
        return "success";

    }

    @Override
    public List<UserModel> findAll(UserModel model) {
        UserDao userDao = new UserImpl();
        return userDao.findAll(model);
    }

    @Override
    public UserModel findById(UserModel userModel) {
        UserDao userDao = new UserImpl();

        return userDao.findById(userModel);
    }

    @Override
    public String update(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.update(userModel) > 0 ? "success" : "error";
    }

    @Override
    public String delete(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.delete(userModel) > 0 ? "success" : "error";
    }

    @Override
    public String updateEnable(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.updateEnable(userModel) > 0 ? "success" : "error";
    }

    @Override
    public int getCount(UserModel model) {
        UserDao userDao = new UserImpl();
        return userDao.getCount(model);
    }
}
